package org.ruanjj.getMinSubString;

import java.util.HashMap;
import java.util.Map;

class Solution {
    public void gameOfLife(int[][] board) {
        int m = board.length;
        int n = board[0].length;

        int[][] boardNew = new int[m][n];

        for(int i = 0; i < m ; i++){
            for(int j = 0; j < n; j++){
                int x = 0; //记录该元素周围活细胞的个数
                for(int k = i-1; k <= i+1; k++){
                    if(k >=0 && k < m){
                        for(int l = j-1; l <= j+1; l++){
                            if(l >= 0 && l < n){
                                if(board[k][l] == 1 && !(k == i && l == j)){
                                    x++;
                                }
                            }
                        }
                    }
                }

                boardNew[i][j] = board[i][j];
                if(board[i][j] == 1 && (x < 2 || x > 3)){
                    boardNew[i][j] = 0;
                }else if(board[i][j] == 0 && x == 3){
                    boardNew[i][j] = 1;
                }
            }
        }


        for(int i = 0; i < m ; i++){
            for(int j = 0; j < n; j++){
                board[i][j] = boardNew[i][j];
            }
        }
    }
}
